<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Trivial logging with filters</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Log v2">
<link rel="up" href="../tutorial.html" title="Tutorial">
<link rel="prev" href="../tutorial.html" title="Tutorial">
<link rel="next" href="sinks.html" title="Setting up sinks">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../tutorial.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="sinks.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="log.tutorial.trivial_filtering"></a><a class="link" href="trivial_filtering.html" title="Trivial logging with filters">Trivial logging with
      filters</a>
</h3></div></div></div>
<p>
        While severity levels can be used for informative purposes, you will normally
        want to apply filters to output only significant records and ignore the rest.
        It is easy to do so by setting a global filter in the library core, like
        this:
      </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">init</span><span class="special">()</span>
<span class="special">{</span>
    <span class="identifier">logging</span><span class="special">::</span><span class="identifier">core</span><span class="special">::</span><span class="identifier">get</span><span class="special">()-&gt;</span><span class="identifier">set_filter</span>
    <span class="special">(</span>
        <span class="identifier">logging</span><span class="special">::</span><span class="identifier">trivial</span><span class="special">::</span><span class="identifier">severity</span> <span class="special">&gt;=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">trivial</span><span class="special">::</span><span class="identifier">info</span>
    <span class="special">);</span>
<span class="special">}</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*[])</span>
<span class="special">{</span>
    <span class="identifier">init</span><span class="special">();</span>

    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">trace</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"A trace severity message"</span><span class="special">;</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">debug</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"A debug severity message"</span><span class="special">;</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">info</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"An informational severity message"</span><span class="special">;</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">warning</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"A warning severity message"</span><span class="special">;</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">error</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"An error severity message"</span><span class="special">;</span>
    <span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">fatal</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">"A fatal severity message"</span><span class="special">;</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
<p>
        <a href="../../../../../../libs/log/example/doc/tutorial_trivial_flt.cpp" target="_top">See the
        complete code</a>.
      </p>
<p>
        Now, if we run this code sample, the first two log records will be ignored,
        while the remaining four will pass on to the console.
      </p>
<div class="important"><table border="0" summary="Important">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td>
<th align="left">Important</th>
</tr>
<tr><td align="left" valign="top"><p>
          Remember that the streaming expression is only executed if the record passed
          filtering. Don't specify business-critical calls in the streaming expression,
          as these calls may not get invoked if the record is filtered away.
        </p></td></tr>
</table></div>
<p>
        A few words must be said about the filter setup expression. Since we're setting
        up a global filter, we have to acquire the <a class="link" href="../detailed.html#log.detailed.core.core" title="Logging core">logging
        core</a> instance. This is what <code class="computeroutput"><span class="identifier">logging</span><span class="special">::</span><span class="identifier">core</span><span class="special">::</span><span class="identifier">get</span><span class="special">()</span></code>
        does - it returns a pointer to the core singleton. The <code class="computeroutput"><span class="identifier">set_filter</span></code>
        method of the logging core sets the global filtering function.
      </p>
<p>
        The filter in this example is built as a <a href="https://www.boost.org/doc/libs/release/libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
        lambda expression. In our case, this expression consists of a single logical
        predicate, whose left argument is a placeholder that describes the attribute
        to be checked, and the right argument is the value to be checked against.
        The <code class="computeroutput"><span class="identifier">severity</span></code> keyword is a
        placeholder provided by the library. This placeholder identifies the severity
        attribute value in the template expressions; this value is expected to have
        name "Severity" and type <code class="computeroutput"><a class="link" href="../reference.html#boost.log.trivial.severity_level">severity_level</a></code>. This attribute
        is automatically provided by the library in case of trivial logging; the
        user only has to supply its value in logging statements. The placeholder
        along with the ordering operator creates a function object that will be called
        by the logging core to filter log records. As a result, only log records
        with severity level not less than <code class="computeroutput"><span class="identifier">info</span></code>
        will pass the filter and end up on the console.
      </p>
<p>
        It is possible to build more complex filters, combining logical predicates
        like this with each other, or even define your own function (including a
        C++11 lambda function) that would act as a filter. We will return to filtering
        in the following sections.
      </p>
</div>
<div class="copyright-footer">Copyright © 2007-2024 Andrey Semashev<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>).
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../tutorial.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="sinks.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
